/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.ReservationController;
import java.beans.PropertyChangeEvent;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.text.MaskFormatter;

public class ConfirmerReservation extends AbstractViewPanel {
    
    private ReservationController controller;
    /**
     * Creates new form ConfirmerReservation
     */
    public ConfirmerReservation(ReservationController controller) {
        
        this.controller = controller;
        initComponents();
    }
    
    public void updateLigneReservation(){
        controller.updateLigneReservation();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNomClientTitre = new javax.swing.JLabel();
        lblNumtelTitre = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        btnOK = new javax.swing.JButton();
        txtNomClient = new javax.swing.JTextField();
        MaskFormatter mf = null;
        try{
            mf = new MaskFormatter("###-###-####");
            mf.setPlaceholder("_");
        }catch (Exception e) {
            e.printStackTrace();
        }
        txtNumTel = new JFormattedTextField(mf);
        jScrollPane1 = new javax.swing.JScrollPane();
        tblLigneReservation = new javax.swing.JTable();

        lblNomClientTitre.setText("Nom du client:");

        lblNumtelTitre.setText("Numéro Téléphone:");

        btnCancel.setText("Annuller");
        btnCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCancelMouseClicked(evt);
            }
        });

        btnOK.setText("OK");
        btnOK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnOKMouseClicked(evt);
            }
        });

        txtNomClient.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNomClientFocusLost(evt);
            }
        });

        txtNumTel.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNumTelFocusLost(evt);
            }
        });

        tblLigneReservation.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblLigneReservation);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(228, 228, 228)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblNomClientTitre)
                                    .addComponent(lblNumtelTitre))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtNomClient)
                                    .addComponent(txtNumTel, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(42, 42, 42)
                                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnCancel)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNomClientTitre)
                    .addComponent(txtNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNumtelTitre)
                    .addComponent(txtNumTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK)
                    .addComponent(btnCancel))
                .addContainerGap(23, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnOKMouseClicked
        controller.updateNumConfirmation();
    }//GEN-LAST:event_btnOKMouseClicked

    private void txtNomClientFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNomClientFocusLost
       try{
           controller.changeNomClient(txtNomClient.getText());
       }catch (Exception e){
           //handle exception
       }
    }//GEN-LAST:event_txtNomClientFocusLost

    private void txtNumTelFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNumTelFocusLost
       try{
           controller.changeNumClient(txtNumTel.getText());
       }catch (Exception e){
           //handle exception
       }
    }//GEN-LAST:event_txtNumTelFocusLost

    private void btnCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelMouseClicked
        controller.deleteReservation();
        this.getTopLevelAncestor().setVisible(false);
    }//GEN-LAST:event_btnCancelMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNomClientTitre;
    private javax.swing.JLabel lblNumtelTitre;
    private javax.swing.JTable tblLigneReservation;
    private javax.swing.JTextField txtNomClient;
    private javax.swing.JFormattedTextField txtNumTel;
    // End of variables declaration//GEN-END:variables

    private void populerTable(String[][] ligneReservation){
        
        String[] celNames;
        
        celNames = new String[4];
        celNames[0] = "Nombre de chambre";
        celNames[1] = "Nom de la catégorie";
        celNames[2] = "Date d'arrivée";
        celNames[3] = "Date de départ";
        
        JTable jtbl = new JTable(ligneReservation, celNames);
        
        this.tblLigneReservation.setModel(jtbl.getModel());        
    }
    
    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        
        switch(evt.getPropertyName()){
            case ReservationController.NUM_CONFIRMATION_PROPERTY:
                this.getTopLevelAncestor().setVisible(false);
                JOptionPane.showMessageDialog(this, "Le numéro de la confirmation est : " + evt.getNewValue().toString(), "La réservation est terminée", 2);        
                break;
            case ReservationController.CLIENT_NOM_PROPERTY:
                txtNomClient.setText(evt.getNewValue().toString());
                break;
            case ReservationController.CLIENT_NUM_PROPERTY:
                txtNumTel.setText(evt.getNewValue().toString());
                break;
            case ReservationController.LIGNE_RESERVATION_PROPERTY:
                String[][] data = (String[][]) evt.getNewValue();
                populerTable(data);
                break;
            case ReservationController.CLIENT_PROPERTY:
                this.getTopLevelAncestor().setVisible(false);
                break;
        }
    }
}
